package com.hh.config;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.util.Map;

/**
 * @author hanhui
 * @version 1.0
 * @description: TODO
 * @date 2025/9/27 17:55
 **/
@Slf4j
@Component
public class CustomHandshakeHandler extends DefaultHandshakeHandler {
    @Override
    protected Principal determineUser(@NotNull ServerHttpRequest request, @NotNull WebSocketHandler wsHandler, Map<String, Object> attributes) {
        // 获取用户信息
        Object userId = attributes.get("userId");
        if (userId != null) {
            log.info("WebSocket握手成功，用户ID: {}", userId);
            return userId::toString;
        } else {
            log.warn("WebSocket握手失败，未找到用户ID");
        }
        return null;
    }
}